// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ControllerDeviceManager.java

package keel.runtime.ctrl;

import java.util.*;
import net.java.games.input.ControllerEnvironment;

// Referenced classes of package keel.runtime.ctrl:
//            ControllerDevice

public final class ControllerDeviceManager
{

    public static ControllerDeviceManager getManager()
    {
        return singleton;
    }

    private ControllerDeviceManager()
    {
    }

    public synchronized List scanControllerDevices()
    {
        if(controllerDevices != null)
        {
            for(int i = 0; i < controllerDevices.length; i++)
                controllerDevices[i].dispose();

        }
        controllerDevices = scanControllerDevicesNative();
        return new ArrayList(Arrays.asList(controllerDevices));
    }

    public synchronized List getControllerDevices()
    {
        if(controllerDevices == null)
            return scanControllerDevices();
        else
            return new ArrayList(Arrays.asList(controllerDevices));
    }

    public synchronized ControllerDevice searchControllerDevice(String id)
    {
        if(controllerDevices == null)
            return null;
        for(int i = 0; i < controllerDevices.length; i++)
        {
            ControllerDevice device = controllerDevices[i];
            if(id.equals(device.getDeviceID()))
                return device;
        }

        return null;
    }

    private ControllerDevice[] scanControllerDevicesNative()
    {
    	net.java.games.input.Controller ctrls[]=ControllerEnvironment.getDefaultEnvironment().getControllers();

    	ArrayList ctrlList=new ArrayList();
    	for(int i=0;i<ctrls.length;++i)
    	{
    		if(ctrls[i].getType()==net.java.games.input.Controller.Type.STICK)
    		{
    			ctrlList.add(new backstage.runtime.ctrl.ControllerDeviceImpl(ctrls[i]));
    		}
    	}
    	if(ctrlList.isEmpty())
    		return new ControllerDevice[0];
    	return (ControllerDevice[])ctrlList.toArray(new ControllerDevice[0]);
    }

    private static ControllerDeviceManager singleton = new ControllerDeviceManager();
    private ControllerDevice controllerDevices[];
   
}
